package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.dao.SetMealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.Setmeal;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    SetMealDao setMealDao;

    @Override
    public Result weiXinOrder(Map<String, Object> map) throws Exception {
        String orderDate = (String) map.get("orderDate");
        Integer setmealId = Integer.parseInt(map.get("setmealId").toString());
        String telephone = (String) map.get("telephone");
        Date date = DateUtils.parseString2Date(orderDate);
        OrderSetting orderSetting = orderSettingDao.findOrderByOrderDate(date);
        //检查所选日期是否已经提前进行了预约设置
        if (orderSetting == null) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        //检查所选日期是否已经预约满
        if (reservations >= number) {
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        //检查是否重复预约,同一个用户在同一天预约同一个套餐
        Member member = memberDao.findByTelephone(telephone);
        if (member != null) {
            Integer memberId = member.getId();
            Order order = new Order(memberId, date, setmealId);
            List<Order> orders = orderDao.findByCondition(order);
            if (orders.size()>0){
                return new Result(false,MessageConstant.HAS_ORDERED);
            }
        }
        member = new Member();
        member.setName((String) map.get("name"));
        member.setPhoneNumber(telephone);
        member.setIdCard((String) map.get("idCard"));
        member.setSex((String) map.get("sex"));
        member.setRegTime(new Date());
        memberDao.add(member);

        //预约
        Order order = new Order();
        //设置会员ID
        order.setMemberId(member.getId());
        //预约日期
        order.setOrderDate(date);
        //预约类型
        order.setOrderType((String) map.get("orderType"));
        //到诊状态
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        //套餐ID
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));
        orderDao.add(order);
        //设置已预约人数+1
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingDao.editOrderDate(orderSetting);

        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }

    @Override
    public Order findByid(Integer id) {
        return orderDao.findByid(id);
    }

    @Override
    public PageResult findOrdersByCurrentDay(String date, QueryPageBean pageBean) {
        List<Map<String,Object>> list = new ArrayList<>();
        Integer currentPage = pageBean.getCurrentPage();
        Integer pageSize = pageBean.getPageSize();
        Page<Order> orderList = PageHelper.startPage(currentPage, pageSize);
        orderDao.findOrdersByCurrentDay(date);
        for (Order order : orderList) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("id",order.getId());
            map.put("orderType",order.getOrderType());
            map.put("orderStatus",order.getOrderStatus());
            Integer memberId = order.getMemberId();
            Member member = memberDao.findById(memberId);
            map.put("memberName",member.getName());
            Integer setmealId = order.getSetmealId();
            Setmeal setmeal = setMealDao.findById(setmealId);
            map.put("setmealName",setmeal.getName());
            list.add(map);
        }
        long total = orderList.getTotal();
        return new PageResult(total,list);
    }
}
